<?php
/**
 * 用户信息操作类
 * @author:zhulin
 * @since:2012 4.17
 * 
 */
class User_Info
{
    
    const TABLE_NAME = 'userinfo';
    const TABLE_NAME_SEARCH = 'userinfo';
    
    
    /**
     * 根据用户名用户密码获取用户信息
     * @param type $Uname   用户名
     * @return type 
     */
    
    public static function getUserInfo( $Uname,$Password)
    {
        if( $Uname == ''|| $Password == '' )
        {
            throw new AppException( 303 );
        }
                $replace = array(
            '[' => '/[',
            ']' => '/]',
            '%' => '/%',
            '_' => '/_',
        );
        $Uname = strtr( $Uname , $replace);
        $Password = strtr($Password , $replace);
        $userInfo = Common::getMysql()->get( self::TABLE_NAME_SEARCH , array( 'Uname' => $Uname , 'Passwd' => $Password ) );
        return $userInfo;
    } 
    /**
     * 获取用户的详细信息
     * 
     */
    public static function getUser ( $UId ) {
        
        return Common::getMysql()->get( self::TABLE_NAME_SEARCH , array( 'UId' => $UId ) );
        
    }
    /**
     *
     * @param type $name
     * @param type $passwd
     * 
     */
    
    public static function userRegister( $name , $password , $brithday , $sex , $phone  ,$email   )
    {
        //参数校验
        if( empty( $name ) || empty( $password ) || empty ( $brithday ) || empty ( $sex )|| empty ( $phone )|| empty ( $email )  )
        {
            echo "不能为空";
            // throw new AppException( 303 );
        }
         //邮箱验证
        $email = Helper_Validate::emailCheck( $email );
        
        //密码长度验证
        $password = Helper_Validate::passwordCheck( $password );

        //检验邮箱是否已经被注册
        if( self::emailIsExists( $email ) )
       {
            echo "邮箱已存在";
            //throw new AppException( 2000 , $email );
        }
 
        //加密密码
         $key = Common::getConfig( 'passEncrypt' );
         $passwdHash = Helper_String::password_hash( $password , $key );
         
       
       
        //用户初始信息
        $userInfo = array(
            'UId'      => Counter_Builder::getInstance()->getUniqueIdByKey( 'UId' ) ,
            'UName'    => $name ,
            'Passwd'   => $passwdHash ,
            'BrithDay' => $brithday,
            'Sex'      => $sex,
            'Phone'    => $phone,
            'Email'    => $email   
        );
       
        //添加用户信息到数据库
        $affectedRows = Common::getMysql()->add( self::TABLE_NAME ,$userInfo );
        if( $affectedRows < 1 )
        {
            echo "添加失败";
           throw new AppException( 1000 );
        }
      //返回用户信息
       return $userInfo;
    }
    /**
     * 判断用户设置的邮箱是否存在
     *
     * @param string    $email
     * @return  boolean
     */
    public static function emailIsExists( $email )
    {
        if( empty( $email ) )
        {
            echo "empty";
           // throw new AppException( 303 );
        }
       
        //邮箱地址过滤
        $email = Helper_Validate::emailCheck( $email );
       

        //根据邮箱查找用户
       $count = Common::getMysql()->getCount( self::TABLE_NAME_SEARCH , array( 'Email' => $email ) );
        
        return $count ? true : false;
    }


    
}